package session

import (
	"egin/pkg/config"
	"egin/pkg/redigo"

	"github.com/boj/redistore"
	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-contrib/sessions/memstore"
	"github.com/gin-contrib/sessions/redis"
	"go.uber.org/zap"
)

//Store session对象
var store sessions.Store

func init() {

	switch config.C().Session.Store {
	case "redis":
		store, _ = redis.NewStoreWithPool(redigo.GetPool(), []byte("secret"))
		if config.C().Session.StorageFormat == "json" {
			_, rediStore := redis.GetRedisStore(store)
			rediStore.SetSerializer(redistore.JSONSerializer{})
		}
	case "cookie":
		store = cookie.NewStore([]byte("secret"))
	case "memstore":
		store = memstore.NewStore([]byte("secret"))
	default:
		zap.L().Error("session store error")
	}

	store.Options(sessions.Options{
		Path:   "/",
		MaxAge: config.C().Session.MaxAge,
	})
}

func GetStore() sessions.Store {

	return store
}
